package service

import (
	"fmt"
	"gin/types"
	"strconv"
)

type ProductionCard struct {
}

// cardList
func (P ProductionCard) StepCategoryCardList(episodeData []types.EpisodeData, EpisodeTaskData map[string]types.EpisodeTask,
	EpisodeTaskNotTimeDataMap map[string]types.EpisodeTask, MakingDataArrayGroupMap, makingNotTimeData map[string]types.MakingList,
	OverdueNumDataMap, OverdueNumNotTimeData map[string]types.OverDueNumRes, StepTaskData []types.StepTaskInfoRes,
	episodeStepCategoryPlanMap map[string]planData, stepCategoryNotWorkOrder map[string]notCloseWorkOrder,
	GetStepCategoryFinishTaskData, GetStepCategoryFinishTaskNotTimeData map[string]types.StepCategoryFinishTask,
	GetStepCategoryRepairIngData map[string]types.StepCategoryRepairIng, GetEpisodeJsonData map[uint64]types.EpisodeJson, GetStepCategoryRepairNumData map[string]types.StepCategoryRepairNum,
	Req types.ProductionManageReq) (res []interface{}, err error) {

	for _, Item := range episodeData {

		var episodePlanSealTime string
		var episodeShowTime string
		var CardInfoLists = make(map[string]interface{}) // 卡片列表
		var NotCensusColumns []ColumnItem                // 不参与统计列
		var ColumnItemData []ColumnItem                  // 参与统计列
		var EpisodeIsSeal string                         // 是否封板
		for _, StepItem := range StepTaskData {

			var NotClosedWorkOrder uint64
			matchKey := fmt.Sprintf("%d-%d", Item.EpisodeId, StepItem.StepCategoryId)
			var planEndTime int64
			if _, ok := episodeStepCategoryPlanMap[matchKey]; ok {
				planEndTime = episodeStepCategoryPlanMap[matchKey].EndTime
			}
			if _, ok := stepCategoryNotWorkOrder[matchKey]; ok {
				NotClosedWorkOrder = stepCategoryNotWorkOrder[matchKey].Num
			}
			// 已完成任务-区间
			var stepCategoryFinishTaskNum uint64
			if _, ok := GetStepCategoryFinishTaskData[matchKey]; ok {
				stepCategoryFinishTaskNum = GetStepCategoryFinishTaskData[matchKey].Num
			}
			// 已完成任务-总计
			var stepCategoryFinishTaskNotTimeNum uint64
			if _, ok := GetStepCategoryFinishTaskNotTimeData[matchKey]; ok {
				stepCategoryFinishTaskNotTimeNum = GetStepCategoryFinishTaskNotTimeData[matchKey].Num
			}
			var stepCategoryRepairIngNum uint64
			if _, ok := GetStepCategoryRepairIngData[matchKey]; ok {
				// 返修中
				stepCategoryRepairIngNum = GetStepCategoryRepairIngData[matchKey].RepairIngNum
			}
			if _, ok := GetEpisodeJsonData[Item.EpisodeId]; ok {
				episodePlanSealTime = GetEpisodeJsonData[Item.EpisodeId].EpisodePlanSealTime
				episodeShowTime = GetEpisodeJsonData[Item.EpisodeId].EpisodePlanShowTime
				EpisodeIsSeal = GetEpisodeJsonData[Item.EpisodeId].EpisodeIsSeal // 是否封版
			}
			var episodeTaskDataNum uint64
			if _, ok := EpisodeTaskData[matchKey]; ok {
				episodeTaskDataNum = EpisodeTaskData[matchKey].Num
			}
			var RepairRateNum float64 // 返修率 = 返修次数 / 总任务数
			if _, ok := GetStepCategoryRepairNumData[matchKey]; ok {
				if GetStepCategoryRepairNumData[matchKey].RepairNum > 0 && episodeTaskDataNum > 0 {
					tmpNum := ((float64(GetStepCategoryRepairNumData[matchKey].RepairNum)) / float64(episodeTaskDataNum)) * 100
					RepairRateNum, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", tmpNum), 64)
				}
			}
			var EpisodeTaskNotTimeNum uint64
			if _, ok := EpisodeTaskNotTimeDataMap[matchKey]; ok {
				EpisodeTaskNotTimeNum = EpisodeTaskNotTimeDataMap[matchKey].Num
			}
			var makingNum uint64
			if _, ok := MakingDataArrayGroupMap[matchKey]; ok {
				makingNum = MakingDataArrayGroupMap[matchKey].MakingNum
			}
			var makingNotTimeNum uint64
			if _, ok := makingNotTimeData[matchKey]; ok {
				makingNotTimeNum = makingNotTimeData[matchKey].MakingNum
			}
			var OverdueNum uint64
			if _, ok := OverdueNumDataMap[matchKey]; ok {
				OverdueNum = OverdueNumDataMap[matchKey].OverdueNum
			}
			var OverdueNumNotTimeNum uint64
			if _, ok := OverdueNumNotTimeData[matchKey]; ok {
				OverdueNumNotTimeNum = OverdueNumNotTimeData[matchKey].OverdueNum
			}
			var TaskStatisticsItem, FilterTaskStatisticsItem TaskStatistics

			var Schedule float64
			if episodeTaskDataNum > 0 { // 计算进度
				Schedule = (float64(stepCategoryFinishTaskNum) / float64(episodeTaskDataNum)) * 100
				Schedule, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", Schedule), 64)
			}
			var ScheduleNotTime float64
			if EpisodeTaskNotTimeNum > 0 { // 计算进度
				ScheduleNotTime = (float64(stepCategoryFinishTaskNotTimeNum) / float64(EpisodeTaskNotTimeNum)) * 100
				ScheduleNotTime, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", ScheduleNotTime), 64)
			}
			// 周期内-有时间筛选条件
			FilterTaskStatisticsItem = TaskStatistics{
				Total:    episodeTaskDataNum,
				MakeIng:  makingNum,
				Overdue:  OverdueNum,
				Schedule: Schedule,
			}
			if Req.StartTime <= 0 && Req.EndTime <= 0 {
				EpisodeTaskNotTimeNum = episodeTaskDataNum
				makingNotTimeNum = makingNum
				OverdueNumNotTimeNum = OverdueNum
				ScheduleNotTime = Schedule // 进度
			}
			// 总计-没有时间筛选条件
			TaskStatisticsItem = TaskStatistics{
				Total:    EpisodeTaskNotTimeNum,
				MakeIng:  makingNotTimeNum,
				Overdue:  OverdueNumNotTimeNum,
				Schedule: ScheduleNotTime, // 进度
			}

			CardInfoLists[StepItem.StepCode] = ListItem{
				Code:               StepItem.StepCode,
				RepairIng:          stepCategoryRepairIngNum, // 返修中
				LinkPlanEndTime:    planEndTime,
				NotClosedWorkOrder: NotClosedWorkOrder,
				RepairRate:         RepairRateNum,
				TotalTask:          TaskStatisticsItem,
				FilterTask:         FilterTaskStatisticsItem,
				StepCategoryId:     StepItem.StepCategoryId,
				StepId:             StepItem.StepId,
				IsStandard:         StepItem.IsStandard,
				StepName:           StepItem.StepName,
				IsEnableCensus:     StepItem.IsEnableCensus, // 是否支持数据统计
			}
			if StepItem.IsEnableCensus == "no" {
				NotCensusColumns = append(NotCensusColumns, ColumnItem{
					Name:  StepItem.ShowName,
					Code:  StepItem.StepCode,
					Index: StepItem.ShowIndex,
				})
			}
			ColumnItemData = append(ColumnItemData, ColumnItem{
				Name:  StepItem.StepName,
				Code:  StepItem.StepCode,
				Index: StepItem.ShowIndex,
			})
		}
		var tmpRes = ProductionManageRes{
			EpisodeInfo: EpisodeInfo{
				Id:            Item.EpisodeId,
				Name:          Item.EntityCode,
				Deliver:       episodePlanSealTime,
				Play:          episodeShowTime,
				EpisodeIsSeal: EpisodeIsSeal,
			},
			CardInfoList:    CardInfoLists, // 卡片集合
			Column:          ColumnItemData,
			NotCensusColumn: NotCensusColumns,
		}
		res = append(res, tmpRes)
	}
	return
}
